<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
	response.setHeader("X-Frame-Options","SAMEORIGIN");
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"+ request.getServerName() + ":" + request.getServerPort()+ path + "/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>中国安全防范技术行业协会后台管理系统</title>
  <jsp:include page="../../public/public.jsp"/>
</head>



<body class="easyui-layout">

		<table id="menuTreeDg" class="easyui-treegrid" style="width:100%;height:100%"
			data-options="
				url: '<%=basePath %>menu/getAllMenu?id=1',
				method: 'post',
				rownumbers: true,
				idField: 'id',
				treeField: 'text',
				toolbar:toolbar
			">
		<thead>
			<tr>
				<th data-options="field:'text',width:240,align:'left'">菜单名称</th>
				<th data-options="field:'id',width:80,align:'center'">ID</th>
				<th data-options="field:'parentId',width:100,align:'center'">父级ID</th>
				<th data-options="field:'qianUrl',width:240,align:'center'">前台地址</th>
				<th data-options="field:'houUrl',width:240,align:'center'">后台地址</th>
				<th data-options="field:'yesNo',width:100,align:'center',formatter:fn_status_formatter">前台是否显示</th>
				<th data-options="field:'orderId',width:60,align:'left'">菜单顺序</th>
			</tr>
		</thead>
	</table>
<div id="win">

	
		 <div style="margin-left: 5%;margin-top: 3%">
		 
		 <table cellpadding="5">
		 	<tr>
		 		<td>菜单名称:</td>
		 		<td colspan="2">
		 			<input type="hidden" id="indexId"/>
		 			<input class="easyui-textbox" id="menuName" type="text" prompt="该项为必填值" missingMessage="不能为空"  data-options="required:true" style="width:200px"></input>
		 		</td>
		 	</tr>
		 	
		 	<tr>
		 		<td>上级菜单:</td>
		 		<td colspan="2">
		 			<input id="treecc" value="01">  
		 		</td>
		 	</tr>
		 	<tr>
		 		<td>前台地址:</td>
		 		<td colspan="2">
		 			<input id="qianUrl" class="easyui-textbox" type="text"    style="width:200px"></input>
		 		</td>
		 	</tr>
		 	<tr>
		 		<td>后台地址:</td>
		 		<td colspan="2">
		 			<input id="houUrl" class="easyui-textbox" type="text"     style="width:200px"></input>
		 		</td>
		 	</tr>
		 	<tr>
		 		<td>前台是否显示:</td>
		 		<td colspan="2">
		 			<select id="showcc" class="easyui-combobox" data-options="panelHeight:'auto'" style="width:50px;">   
					    <option value=0 selected="selected">否</option>   
					    <option value=1>是</option>
					    
					</select>  
		 		</td>
		 	</tr>
		 	<tr>
		 		<td>菜单顺序:</td>
		 		<td colspan="2">
		 			
		 			<input class="easyui-textbox" id="orderId" type="text" style="width:50px"></input>
		 		</td>
		 	</tr>
		 
		 <tr>
	    			<td colspan="3" align="middle" style="padding-left:20px">
	    			  <a id="saveMenu" href="javascript:void(0)" data-options="iconCls:'icon-save'" class="easyui-linkbutton" onclick="fn_save()">保存</a>
	    			  <a id="upMenu" href="javascript:void(0)" data-options="iconCls:'icon-edit'" class="easyui-linkbutton" onclick="fn_update()">修改</a>
	    			  <a href="javascript:void(0)" data-options="iconCls:'icon-cancel'" class="easyui-linkbutton" onclick="fn_cancel()">取消</a>
	    			</td>
	    		</tr>
		 </table>
		 </div>
		     
		  

	   
</div> 

	<script type="text/javascript">
	
	/* 控制修改按钮重复点击 */
	var upstats=-1;
	/**
	* 
	*点击修改触发
	*
	*/
	function fn_update(){
		
		
		if(upstats==1){
			$.messager.alert("提示","正在修改，请耐心等待","error");
			return;
		}
		
		upstats=1;
		
		var id = $("#indexId").val();
		var t = $('#treecc').combotree('tree');
		var n = t.tree('getSelected');
		var treeccVal = n.id;//提交的时候提交id就行了  这是当前菜单的  父id
		var menuName = $("#menuName").val().replace(/\s/g, "");//菜单名称
		var qianUrl = $("#qianUrl").val();//前台地址
		var houUrl = $("#houUrl").val();//后台地址
		var showcc = $("#showcc").combobox('getValue');//是否在前台显示
		var orderId = $("#orderId").val().replace(/\s/g, "");
		if(menuName==null || menuName==""){
			$.messager.alert("提示","菜单名称为必填项","error");
			upstats = -1;
			return;
		}
		
		
		$.ajax({  
			url:'<%=basePath %>menu/updateMenu',
			type:'post',
			data:{
				'id':id,
				'parentId':treeccVal,
				'text':menuName,
				'qianUrl':qianUrl,
				'houUrl':houUrl,
				'yesNo':showcc,
				'orderId':orderId
			},
			dataType:'json',
			error:function(){
				
				$.messager.alert("提示","修改菜单异常","error");
				upstats=-1;
			}, 
			success:function(r){ 
				if(r!=-1){
					$.messager.alert("提示","修改菜单成功","info");
					$('#menuTreeDg').treegrid('reload');	//  重新刷新  树形表格
					$('#win').window('close');  // 成功后关闭窗口
					upstats=-1;
					$('#treecc').combotree({    
					    url: '<%=basePath %>menu/getAllMenu?id=0',    
					    required: true ,
					    width:"180px",
					    editable:false
					});
				}else{
					$.messager.alert("提示","修改菜单失败","error");
					upstats=-1;
				}
			}  
		}); 
		
	}
	
	/* 控制保存的频繁点击 */
	var stats = -1;
	/* **
	*点击保存触发
	*
	*/
	function fn_save(){
		
		if(stats==1){
			$.messager.alert("提示","正在添加，请耐心等待","error");
			return;
		}
		
		stats=1;
		
		var t = $('#treecc').combotree('tree');
		var n = t.tree('getSelected');
		var treeccVal = n.id;//提交的时候提交id就行了 这是当前菜单的  父id
		var menuName = $("#menuName").val().replace(/\s/g, "");//菜单名称
		var qianUrl = $("#qianUrl").val();//前台地址
		var houUrl = $("#houUrl").val();//后台地址
		var showcc = $("#showcc").combobox('getValue');//是否在前台显示
		
		if(menuName==null || menuName==""){
			$.messager.alert("提示","菜单名称为必填项","error");
			stats = -1;
			return;
		}
		
		$.ajax({  
			url:'<%=basePath %>menu/saveMenu',
			type:'post',
			data:{
				'parentId':treeccVal,
				'text':menuName,
				'qianUrl':qianUrl,
				'houUrl':houUrl,
				'yesNo':showcc
			},
			dataType:'json',
			error:function(){
				
				$.messager.alert("提示","添加菜单异常","error");
				stats=-1;
			}, 
			success:function(r){ 
				if(r==1){
					
					$.messager.alert("提示","添加菜单成功","info");
					$('#menuTreeDg').treegrid('reload');	//  重新刷新  树形表格
					$('#win').window('close');  // 成功后关闭窗口
					stats=-1;
					$('#treecc').combotree({    
					    url: '<%=basePath %>menu/getAllMenu?id=0',    
					    required: true ,
					    width:"180px",
					    editable:false
					});
				}else{
					$.messager.alert("提示","添加菜单失败","error");
					stats=-1;
				}
			}  
		}); 
		
	}
	
	/* 取消按钮  关闭弹窗 */
	function fn_cancel(){
		$('#win').window('close');  // 成功后关闭窗口
	}
	
	/* 是否显示 */
	 function fn_status_formatter(val,rec){
		  if(val=="0"){
			  return "<span style='color:red'>隐藏</span>";
		  }else{
			  return "<span style='color:blue'>显示</span>";
		  }
	  }
	
	$(function(){
		/* 上级菜单  树形下拉框获取菜单数据 */
		$('#treecc').combotree({    
		    url: '<%=basePath %>menu/getAllMenu?id=0',    
		    required: true ,
		    width:"180px",
		    editable:false
		});
	})
	/* *
	*
	*图标按钮
	*
	* */
	 var toolbar = [{
			text:'添加',
			iconCls:'icon-add',
			handler:function(){
				$("#menuName").textbox("setValue",'');
				$("#qianUrl").textbox("setValue",'');
				$("#houUrl").textbox("setValue",'');
				
				/* 增加弹窗 */
				$("#saveMenu").show();
				$("#upMenu").hide();
				$('#win').window({
					title:"增加菜单",
				    width:"400",    
				    height:"300",    
				    modal:true   
				});
				
		 
			}
		},{
			text:'修改',
			iconCls:'icon-edit',
			handler:function(){
				var row = $('#menuTreeDg').datagrid('getSelected');
				if(row==null){
					$.messager.alert("提示","请先选中一条记录","error");
					return;
				}
				var n = $('#menuTreeDg').treegrid('getSelected');
				$("#saveMenu").hide();
				$("#upMenu").show();
				$('#win').window({
					title:"修改菜单",
				    width:"400",    
				    height:"300",    
				    modal:true   
				});
				
				$("#indexId").val(n.id);//把当前菜单的id存进隐藏域
				$("#menuName").textbox("setValue",n.text);
				$("#qianUrl").textbox("setValue",n.qianUrl);
				$("#orderId").textbox("setValue",n.orderId);
				$("#houUrl").textbox("setValue",n.houUrl);
				$('#showcc').combobox('setValue', n.yesNo);
				/* 上级菜单  树形下拉框获取菜单数据 */
				/* 设置已经添加过的值    作为默认值 */
				$('#treecc').combotree('setValue',n.parentId);
			}
		}];



	</script>
</body>
</html>